﻿using Factory.MasterData.Materials.Dto;
using Factory.WarehouseManagement.WarehouseSettings.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.WarehouseManagement.WarehouseSettings
{
    public class BinSetAppService:ApplicationService, IBinSetAppService
    {
        private readonly IRepository<BinSet,int> _binSetRepository;

        public BinSetAppService(IRepository<BinSet,int> binSetRepository)
        {
            _binSetRepository = binSetRepository;
        }

        public async Task<PageResult> GetListAsync(string binSetName, string areaCode, int pageIndex = 1, int pageSize = 10)
        {
            var setlist = await _binSetRepository.GetListAsync(x => x.AreaCode == areaCode);

            setlist = setlist.WhereIf(!string.IsNullOrEmpty(binSetName), x => x.BinSetName.Contains(binSetName)).ToList();
            PageResult pageResult = new PageResult();
            pageResult.RowCount = setlist.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = setlist.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }





        public int CreateAsync(BinSet input)
        {
            var warehouseSetlist = _binSetRepository.GetListAsync(x => x.BinSetName == input.BinSetName).Result;

            // 检查是否已存在该单位名称
            if (warehouseSetlist.Count() > 0)
            {
                return -100;  // 表示重复，或者抛出异常等
            }

            var warehouselist = _binSetRepository.InsertAsync(new BinSet
            {
                BinSetName = input.BinSetName,
                BinSetCapacity = input.BinSetCapacity,
                BinSetCode = input.BinSetCode,
                IsEnable = input.IsEnable,
                IsFreeze = input.IsFreeze,
                BinSetMax = input.BinSetMax,
                BinSetUnitX=input.BinSetUnitX,
                BinSetUnitY=input.BinSetUnitY,
                BinSetUnitZ=input.BinSetUnitZ,
                BinSetResult= input.BinSetResult,

            });
            return 1;
        }

        public async Task<int> UpdateAsync(BinSetDTO input)
        {
            var warehouselist = await _binSetRepository.FindAsync(x => x.Id == input.Id);
            if (warehouselist == null)
            {
                // 客户未找到，可以根据需要抛出异常或返回特定值
                return 0;
            }
            warehouselist.BinSetName = input.BinSetName;
            warehouselist.BinSetCapacity = input.BinSetCapacity;
            warehouselist.BinSetCode = input.BinSetCode;
            warehouselist.IsEnable = input.IsEnable;
            warehouselist.IsFreeze = input.IsFreeze;
            warehouselist.BinSetMax = input.BinSetMax;
            warehouselist.BinSetUnitX = input.BinSetUnitX;
            warehouselist.BinSetUnitY = input.BinSetUnitY;
            warehouselist.BinSetUnitZ = input.BinSetUnitZ;
            warehouselist.BinSetResult = input.BinSetResult;
           
            await _binSetRepository.UpdateAsync(warehouselist);
            return 1; // 或者返回其他表示成功的值
        }

        public async Task<int> DeleteAsync(int id)
        {
            var warehouselist = await _binSetRepository.FindAsync(x => x.Id == id);
            if (warehouselist != null)
            {
                await _binSetRepository.DeleteAsync(warehouselist);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public BinSet GetBinSetById(int id)
        {
            var warehouselist = _binSetRepository.FindAsync(id).Result;
            return warehouselist;
        }
    }
}
